﻿//  Key Tracker - An application to keep track of product keys.
//
//  Copyright (c) 2009  Shawn Rakowski
//
//  Permission is hereby granted, free of charge, to any person
//  obtaining a copy of this software and associated documentation
//  files (the "Software"), to deal in the Software without
//  restriction, including without limitation the rights to use,
//  copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following
//  conditions:
//
//  The above copyright notice and this permission notice shall be
//  included in all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//  OTHER DEALINGS IN THE SOFTWARE.
// --------------------------------------------------------------------------//
/*      EditProductsForm.cs 
 *      
 * 
 *      Key Tracker Project   
 *      
 *      CODE FILE DESCRIPTION:
 *      
 *          This code file contains all the classes and methods for the
 *          edit products form that is run when the edit product button 
 *          is pushed on the main form.
 * 
 *      AUTHOR(S):
 *      
 *          Shawn Rakowski
 * 
 *      CLASSES:
 *      
 *          EditProductsForm : Form
 *      
 *      MODIFICATION HISTORY:
 *      
 *          06-AUG-2008     Shawn R.        New Program.
 *          16-MAR-2008     Shawn R.        Reduced limit on product name to 64.
 *          17-MAR-2009     Shawn R.        Changed Font.
 *  
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KeyTracker.Classes;

namespace KeyTracker.Forms
{
    public partial class EditProductsForm : Form
    {
        #region Class Level Variables
        KeyTrackerDatabase keyTrackDB;
        #endregion

        /// <summary>
        /// Construct and Edit Products Form.
        /// </summary>
        /// <param name="keyTrackerDatabase"></param>
        public EditProductsForm(KeyTrackerDatabase keyTrackerDatabase)
        {
            keyTrackDB = keyTrackerDatabase;
            InitializeComponent();
        }

        private void EditProductsForm_Load(object sender, EventArgs e)
        {
            addProductButton.Enabled = false;
            deleteProductButton.Enabled = false;
            existingProductsListBox.Items.AddRange(keyTrackDB.ProductRange);
        }

        private void addProductButton_Click(object sender, EventArgs e)
        {
            if (newProductTextBox.Text != "")
            {
                if (!existingProductsListBox.Items.Contains(newProductTextBox.Text))
                {
                    keyTrackDB.AddProduct(newProductTextBox.Text);
                    existingProductsListBox.Items.Clear();
                    existingProductsListBox.Items.AddRange(keyTrackDB.ProductRange);
                }
                else
                {
                    string message = "That product already exists!\nProduct was not added.";
                    MessageBox.Show(message, "Add Product", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                newProductTextBox.Text = "";
                newProductTextBox.Focus();
            }
        }

        private void deleteProductButton_Click(object sender, EventArgs e)
        {
            if (existingProductsListBox.SelectedIndex != -1)
            {
                string message = "WARNING: deleting this product will delete all keys associated with it. " +
                                 "Are you sure you want to delete " + existingProductsListBox.SelectedItem.ToString() +
                                 "?";
                if (MessageBox.Show(message, "Delete Product", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == 
                    DialogResult.Yes)
                {
                    keyTrackDB.DeleteProduct(existingProductsListBox.SelectedIndex);
                    existingProductsListBox.Items.Clear();
                    existingProductsListBox.Items.AddRange(keyTrackDB.ProductRange);
                }
            }
        }

        private void finishedButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void newProductTextBox_TextChanged(object sender, EventArgs e)
        {
            if (newProductTextBox.Text != "")
            {
                addProductButton.Enabled = true;
            }
            else
            {
                addProductButton.Enabled = false;
            }
        }

        private void existingProductsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (existingProductsListBox.SelectedIndex != -1)
            {
                deleteProductButton.Enabled = true;
            }
            else
            {
                deleteProductButton.Enabled = false;
            }
        }

        private void newProductTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                addProductButton_Click(sender, e);
        }
    }
}
